package com.sgb.kjwl.viewmodel.business;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.business.MemberEntity;
import com.sgb.kjwl.repository.DeviceRepertory;
import com.sgb.kjwl.view.ui.activity.business.MemberListActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MemberListModel extends BaseViewModel {

    private MemberListActivity activity;
    private DeviceRepertory repertory;
    private int page = 1;
    private int size = 20;
    public int total;

    public MemberListModel(MemberListActivity activity, DeviceRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getData(){
        page = 1;
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getBusinessManagementAPI().getMemberList(page, size).enqueue(new Callback<BaseEntity<ProListEntity<List<MemberEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<MemberEntity>>>> call, Response<BaseEntity<ProListEntity<List<MemberEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null || response.body().getData() == null){
                    return;
                }
                total = response.body().getData().getTotalRow();
                activity.showData(response.body().getData().getList());
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<MemberEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getMoreData(){
        page++;
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getBusinessManagementAPI().getMemberList(page, size).enqueue(new Callback<BaseEntity<ProListEntity<List<MemberEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<MemberEntity>>>> call, Response<BaseEntity<ProListEntity<List<MemberEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null){
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    activity.showMoreData(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<MemberEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
